//
//  HYLDrawView.m
//  核心动画-CAKeyFrameAnimation
//
//  Created by hyl on 16/7/5.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLDrawView.h"

@interface HYLDrawView ()
@property (nonatomic,strong) UIBezierPath *path;
@end

@implementation HYLDrawView

- (void)drawRect:(CGRect)rect {
    [self.path stroke];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint p=[touch locationInView:self];
    self.path=[UIBezierPath bezierPath];
    [self.path moveToPoint:p];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint p=[touch locationInView:self];
    [self.path addLineToPoint:p];
    [self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //开启动画
//    CAKeyframeAnimation *keyFrame=[[CAKeyframeAnimation alloc]init];
    CAKeyframeAnimation *keyFrame=[CAKeyframeAnimation animation];
    
    keyFrame.keyPath=@"position";
    keyFrame.path=self.path.CGPath;
    keyFrame.duration=1;
    keyFrame.repeatCount=MAXFLOAT;
    //添加动画
    [self.subviews.firstObject.layer addAnimation:keyFrame forKey:nil];
}
@end
